package com.chenglai.order.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.chenglai.order.pojo.MainOrder;
import com.chenglai.order.pojo.Order;
import com.chenglai.order.service.OrderService;
import com.chenglai.order.service.impl.OrderServiceImpl;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2023/11/28 09:25:54
 * 普通订单接收信息
 **/
@Component
public class MqClient {
    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "orderQueue")
    public void reciveOrder(Map map){
        //接收到订单信息
        String orderNum = map.get("orderNum").toString();
        String userId = map.get("userId").toString();

        MainOrder mainOrder = JSON.parseObject((String)redisTemplate.opsForHash().get("orders_"+userId,"m_"+orderNum),MainOrder.class);

        List<Order> orderList = JSON.parseObject((String) redisTemplate.opsForHash().get("orders_"+userId,"e_"+orderNum),List.class);
        //创建订单

        for (Order order : orderList) {
            order.setOrderNum(orderNum);
            order.setCommentStatus(0);
            order.setShopId(mainOrder.getShopId());
        }
        mainOrder.setOrderList(orderList);
        orderService.addMainOrder(mainOrder);
//        redisTemplate.opsForHash().delete("orders_"+userId);
        System.out.println(map);

    }
}
